#ifndef RTOS_ABSTRACTION_H
#define RTOS_ABSTRACTION_H

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

/* 通用类型定义 */
typedef void* rtos_task_t;
typedef void* rtos_sem_t;
typedef void* rtos_mutex_t;
typedef void* rtos_queue_t;
typedef void* rtos_timer_t;
typedef void* rtos_event_group_t;

/* 错误码定义 */
#define RTOS_OK               0
#define RTOS_ERROR           -1
#define RTOS_TIMEOUT         -2
#define RTOS_INVALID_PARAM   -3
#define RTOS_ISR_CONTEXT     -4

/* 任务优先级定义 */
#define RTOS_PRIORITY_IDLE      0
#define RTOS_PRIORITY_LOWEST    1
#define RTOS_PRIORITY_LOW       2
#define RTOS_PRIORITY_NORMAL    5
#define RTOS_PRIORITY_HIGH      8
#define RTOS_PRIORITY_HIGHEST   10
#define RTOS_PRIORITY_REALTIME  15

/* 等待时间定义 */
#define RTOS_WAIT_FOREVER        0xFFFFFFFFU
#define RTOS_NO_WAIT             0

/* 内存管理 */
void* rtos_malloc(size_t size);
void rtos_free(void *ptr);

/* 系统信息 */
bool rtos_in_isr(void);
uint32_t rtos_get_tick_count(void);
uint32_t rtos_get_tick_frequency(void);

/* 任务管理 */
rtos_task_t rtos_task_create(void (*task_func)(void *), 
                            const char *name,
                            uint32_t stack_size,
                            void *param,
                            uint32_t priority);
void rtos_task_delete(rtos_task_t task);
void rtos_task_delay(uint32_t ms);
void rtos_task_suspend(rtos_task_t task);
void rtos_task_resume(rtos_task_t task);
rtos_task_t rtos_task_self(void);

/* 信号量 */
rtos_sem_t rtos_sem_create(uint32_t initial_count, uint32_t max_count);
int rtos_sem_take(rtos_sem_t sem, uint32_t timeout_ms);
int rtos_sem_give(rtos_sem_t sem);
int rtos_sem_give_from_isr(rtos_sem_t sem);  // 专为ISR设计的信号量释放
void rtos_sem_delete(rtos_sem_t sem);

/* 互斥锁 */
rtos_mutex_t rtos_mutex_create(void);
int rtos_mutex_lock(rtos_mutex_t mutex, uint32_t timeout_ms);
int rtos_mutex_unlock(rtos_mutex_t mutex);
void rtos_mutex_delete(rtos_mutex_t mutex);

/* 消息队列 */
rtos_queue_t rtos_queue_create(uint32_t length, uint32_t item_size);
int rtos_queue_send(rtos_queue_t queue, const void *item, uint32_t timeout_ms);
int rtos_queue_send_from_isr(rtos_queue_t queue, const void *item);  // 专为ISR设计的发送
int rtos_queue_receive(rtos_queue_t queue, void *item, uint32_t timeout_ms);
void rtos_queue_delete(rtos_queue_t queue);

/* 软件定时器 */
rtos_timer_t rtos_timer_create(const char *name,
                              void (*timeout_func)(void *),
                              void *arg,
                              uint32_t timeout_ms,
                              bool auto_reload);
int rtos_timer_start(rtos_timer_t timer);
int rtos_timer_stop(rtos_timer_t timer);
void rtos_timer_delete(rtos_timer_t timer);

/* 事件标志组 */
rtos_event_group_t rtos_event_group_create(void);
uint32_t rtos_event_group_wait(rtos_event_group_t group,
                              uint32_t bits,
                              bool clear_on_exit,
                              bool wait_all,
                              uint32_t timeout_ms);
uint32_t rtos_event_group_set(rtos_event_group_t group, uint32_t bits);
uint32_t rtos_event_group_set_from_isr(rtos_event_group_t group, uint32_t bits);  // 专为ISR设计的设置
uint32_t rtos_event_group_clear(rtos_event_group_t group, uint32_t bits);
void rtos_event_group_delete(rtos_event_group_t group);

/* 系统控制 */
void rtos_start_scheduler(void);
void rtos_yield(void);

#endif /* RTOS_ABSTRACTION_H */